{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/llamaindex/cookbook/blob/main/integrations/Klavis/Use_Klavis_with_LlamaIndex.ipynb)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "# LlamaIndex + Klavis AI Integration\n",
    "\n",
    "This tutorial demonstrates how to build AI agents using LlamaIndex's agent framework with Klavis MCP (Model Context Protocol) servers for enhanced functionality. \n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "## Prerequisites\n",
    "\n",
    "Before we begin, you'll need:\n",
    "\n",
    "- **OpenAI API key** - Get at [openai.com](https://openai.com/)\n",
    "- **Klavis API key** - Get at [klavis.ai](https://klavis.ai/)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip is available: \u001b[0m\u001b[31;49m25.0\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m25.1.1\u001b[0m\n",
      "\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "# Install the required packages\n",
    "%pip install -qU llama-index llama-index-tools-mcp klavis"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "from klavis import Klavis\n",
    "from klavis.types import McpServerName\n",
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.tools.mcp import BasicMCPClient\n",
    "\n",
    "# Set environment variables\n",
    "os.environ[\"OPENAI_API_KEY\"] = \"YOUR_OPENAI_API_KEY\"  # Replace with your actual OpenAI API key\n",
    "os.environ[\"KLAVIS_API_KEY\"] = \"YOUR_KLAVIS_API_KEY\"  # Replace with your actual Klavis API key\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 1: YouTube AI Agent\n",
    "\n",
    "#### Create an AI agent to summarize YouTube videos using LlamaIndex and Klavis MCP Server."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1 - using Klavis to create youtube MCP Server"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\ud83d\udd17 YouTube MCP server created at: https://youtube-mcp-server.klavis.ai/sse?instance_id=270cbd51-e737-407d-85ce-6e6162248671\n"
     ]
    }
   ],
   "source": [
    "klavis_client = Klavis(api_key=os.getenv(\"KLAVIS_API_KEY\"))\n",
    "\n",
    "# Create a YouTube MCP server and get the server URL\n",
    "youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(\n",
    "    server_name=McpServerName.YOUTUBE,\n",
    "    user_id=\"1234\",\n",
    ")\n",
    "\n",
    "youtube_mcp_server_url = youtube_mcp_instance.server_url\n",
    "# print(f\"\ud83d\udd17 YouTube MCP server created at: {youtube_mcp_server_url}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 2 - using Llamaindex to create AI Agent with the MCP Server "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "from llama_index.core.agent.workflow import FunctionAgent\n",
    "from llama_index.tools.mcp import (\n",
    "    get_tools_from_mcp_url,\n",
    "    aget_tools_from_mcp_url,\n",
    ")\n",
    "\n",
    "llm = OpenAI(model=\"gpt-4o-mini\", api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
    "\n",
    "youtube_tools = await aget_tools_from_mcp_url(youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url))\n",
    "\n",
    "youtube_agent = FunctionAgent(\n",
    "    name=\"youtube_agent\",\n",
    "    description=\"Agent using MCP-based tools\",\n",
    "    tools=youtube_tools,\n",
    "    llm=llm,\n",
    "    system_prompt=\"You are an AI assistant that uses MCP tools.\"\n",
    ")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 3 - Run your AI Agent to summarize your favorite video!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The video titled \"Introducing AgentWorkflow, a way to easily create multi-agent systems in Llamaindex\" presents a new system called AgentWorkflow designed for building and orchestrating AI agent systems. It emphasizes the ability to coordinate multiple AI agents while maintaining state and context, making it suitable for both single specialized agents and teams working together.\n",
      "\n",
      "### Key Features:\n",
      "- **Flexible Agent Types**: Includes FunctionAgent and ReActAgent.\n",
      "- **Built-in State Management**: Helps in managing the state of agents effectively.\n",
      "- **Real-time Monitoring**: Allows users to monitor the agents in action.\n",
      "- **Human-in-the-loop Capabilities**: Facilitates human oversight in the agent processes.\n",
      "\n",
      "The video encourages viewers to explore comprehensive tutorials and documentation to learn how to build everything from simple assistants to complex multi-agent systems. \n",
      "\n",
      "### Additional Resources:\n",
      "- Basic Tutorial: [Basic Tutorial](https://docs.llamaindex.ai/en/stable/examples/agent/agent_workflow_basic/)\n",
      "- Full Documentation: [Full docs](https://docs.llamaindex.ai/en/stable/understanding/agent/multi_agents/)\n",
      "- Introductory Blog Post: [Blog Post](https://www.llamaindex.ai/blog/introducing-agentworkflow-a-powerful-system-for-building-ai-agent-systems)\n",
      "- Discord Community: [Join Discord](https://discord.com/invite/eN6D2HQ4aX)\n",
      "\n",
      "The video has a duration of approximately 16 minutes and 38 seconds and has garnered over 7,161 views.\n"
     ]
    }
   ],
   "source": [
    "YOUTUBE_VIDEO_URL = \"https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s\"  # pick a video you like!\n",
    "\n",
    "response = await youtube_agent.run(f\"Summarize this video: {YOUTUBE_VIDEO_URL}\")\n",
    "print(response)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\u2705 Nice work! You\u2019ve successfully oursource your eyeball and summarized your favorite YouTube video!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Case 2: Multi-Agent Workflow\n",
    "\n",
    "#### Build a LlamaIndex AgentWorkflow that summarizes YouTube videos and sends the summary via email."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 1 - using Klavis to create YouTube and Gmail MCP Servers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\u2705 Created YouTube and Gmail MCP instances\n",
      "\ud83d\udd10 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: https://api.klavis.ai/oauth/gmail/authorize?instance_id=d9d482b3-433a-4330-9a8b-9548c0b0a326\n"
     ]
    }
   ],
   "source": [
    "import webbrowser\n",
    "\n",
    "klavis_client = Klavis(api_key=os.getenv(\"KLAVIS_API_KEY\"))\n",
    "\n",
    "# Create YouTube MCP server\n",
    "youtube_mcp_instance = klavis_client.mcp_server.create_server_instance(\n",
    "    server_name=McpServerName.YOUTUBE,\n",
    "    user_id=\"1234\",\n",
    ")\n",
    "\n",
    "# Create Gmail MCP server with OAuth authorization\n",
    "gmail_mcp_instance = klavis_client.mcp_server.create_server_instance(\n",
    "    server_name=McpServerName.GMAIL,\n",
    "    user_id=\"1234\",\n",
    ")\n",
    "\n",
    "print(\"\u2705 Created YouTube and Gmail MCP instances\")\n",
    "\n",
    "# Open Gmail OAuth authorization\n",
    "webbrowser.open(gmail_mcp_instance.oauth_url)\n",
    "print(f\"\ud83d\udd10 Opening OAuth authorization for Gmail, if you are not redirected, please open the following URL in your browser: {gmail_mcp_instance.oauth_url}\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "#### Step 2 - using LlamaIndex to create Multi-Agent Workflow with the MCP Servers\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\ud83e\udd16 Multi-agent workflow created with YouTube and Gmail agents!\n"
     ]
    }
   ],
   "source": [
    "from llama_index.llms.openai import OpenAI\n",
    "from llama_index.core.agent.workflow import FunctionAgent, AgentWorkflow\n",
    "from llama_index.tools.mcp import (\n",
    "    BasicMCPClient,\n",
    "    get_tools_from_mcp_url,\n",
    "    aget_tools_from_mcp_url,\n",
    ")\n",
    "\n",
    "llm = OpenAI(model=\"gpt-4o-mini\", api_key=os.getenv(\"OPENAI_API_KEY\"))\n",
    "\n",
    "# Get MCP server URLs\n",
    "youtube_mcp_server_url = youtube_mcp_instance.server_url\n",
    "gmail_mcp_server_url = gmail_mcp_instance.server_url\n",
    "\n",
    "# Get tools from both MCP servers\n",
    "youtube_tools = await aget_tools_from_mcp_url(youtube_mcp_server_url, client=BasicMCPClient(youtube_mcp_server_url))\n",
    "gmail_tools = await aget_tools_from_mcp_url(gmail_mcp_server_url, client=BasicMCPClient(gmail_mcp_server_url))\n",
    "\n",
    "# Create specialized agents\n",
    "youtube_agent = FunctionAgent(\n",
    "    name=\"youtube_agent\",\n",
    "    description=\"Agent that can summarize YouTube videos\",\n",
    "    tools=youtube_tools,\n",
    "    llm=llm,\n",
    "    system_prompt=\"You are a YouTube video summarization expert. Use MCP tools to analyze and summarize videos.\",\n",
    "    can_handoff_to=[\"gmail_agent\"],\n",
    ")\n",
    "\n",
    "gmail_agent = FunctionAgent(\n",
    "    name=\"gmail_agent\", \n",
    "    description=\"Agent that can send emails via Gmail\",\n",
    "    tools=gmail_tools,\n",
    "    llm=llm,\n",
    "    system_prompt=\"You are an email assistant. Use MCP tools to send emails via Gmail.\"\n",
    ")\n",
    "\n",
    "# Create multi-agent workflow\n",
    "workflow = AgentWorkflow(\n",
    "    agents=[youtube_agent, gmail_agent],\n",
    "    root_agent=\"youtube_agent\",\n",
    ")\n",
    "\n",
    "print(\"\ud83e\udd16 Multi-agent workflow created with YouTube and Gmail agents!\")\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Step 3 - run the workflow!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u2705 Report:\n",
      " The summary of the video \"Introducing AgentWorkflow, a way to easily create multi-agent systems in Llamaindex\" has been successfully sent to zihaolin@klavis.ai. If you need anything else, feel free to ask!\n"
     ]
    }
   ],
   "source": [
    "YOUTUBE_VIDEO_URL = \"https://www.youtube.com/watch?v=MmiveeGxfX0&t=528s\" # pick a video you like!\n",
    "EMAIL_RECIPIENT = \"zihaolin@klavis.ai\"  # Replace with your email\n",
    "\n",
    "resp = await workflow.run(user_msg=f\"Summarize this video {YOUTUBE_VIDEO_URL} and send it to {EMAIL_RECIPIENT}\")\n",
    "print(\"\\n\u2705 Report:\\n\", resp.response.content)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "vscode": {
     "languageId": "raw"
    }
   },
   "source": [
    "\n",
    "## Summary\n",
    "\n",
    "In this tutorial, we explored how to integrate LlamaIndex with Klavis AI to build powerful AI agents using MCP (Model Context Protocol) servers. Here's what we accomplished:\n",
    "\n",
    "### Key Takeaways:\n",
    "\n",
    "1. **Single Agent Setup**: Created a YouTube AI agent that can summarize videos using the Klavis YouTube MCP server\n",
    "2. **Multi-Agent Workflow**: Built a sophisticated workflow combining YouTube and Gmail agents to summarize videos and automatically send summaries via email\n",
    "3. **MCP Integration**: Learned how to use Klavis MCP servers with LlamaIndex's agent framework for enhanced functionality\n",
    "\n",
    "This integration opens up endless possibilities for building AI agents that can interact with various services and platforms through Klavis MCP servers. You can now create agents that work with YouTube, Gmail, GitHub, Slack, and many other services supported by Klavis.\n",
    "\n",
    "Happy building! \ud83d\ude80"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python (cookbook-venv)",
   "language": "python",
   "name": "cookbook-venv"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}